package com.lehe.service.impl;

import com.lehe.dao.WithdrawCashDao;
import com.lehe.entry.SqlParameterDto;
import com.lehe.entry.TransfersTradeDto;
import com.lehe.entry.WithdrawCashDto;
import com.lehe.util.ArithUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * 类的相关描述
 *
 * @author 王丹
 * @since 2018-01-12
 */
@Service
@Transactional
public class WithdrawCashService extends AbstractService {

    @Autowired
    WithdrawCashDao withdrawCashDao;

    public List<WithdrawCashDto> queryIndex(SqlParameterDto sqlParameterDto){
        List<WithdrawCashDto> withdrawCashDtos = withdrawCashDao.queryIndex(sqlParameterDto);
        for(WithdrawCashDto dto : withdrawCashDtos){
            sqlParameterDto.setStartDate(dto.getInputTime());
            sqlParameterDto.setEndDate(null);
            if(null!=dto.getPayCount() && dto.getPayCount()>0){
                List<TransfersTradeDto> transfersTradeDtos = withdrawCashDao.queryTransfersTrade(sqlParameterDto);
                dto.setTradeList(transfersTradeDtos);
            }
            setCnMonth(dto);
        }
        calculate(withdrawCashDtos, sqlParameterDto.getAgentId());
        return withdrawCashDtos;
    }

    private void setCnMonth(WithdrawCashDto dto) {
        String date = dto.getInputTime(); //yyyy-MM
        String month = date.substring(5, 7);
        String cnMonth = "一月";
        if("01".equals(month)){
            cnMonth = "一月";
        }else if("02".equals(month)){
            cnMonth = "二月";
        }else if("03".equals(month)){
            cnMonth = "三月";
        }else if("04".equals(month)){
            cnMonth = "四月";
        }else if("05".equals(month)){
            cnMonth = "五月";
        }else if("06".equals(month)){
            cnMonth = "六月";
        }else if("07".equals(month)){
            cnMonth = "七月";
        }else if("08".equals(month)){
            cnMonth = "八月";
        }else if("09".equals(month)){
            cnMonth = "九月";
        }else if("10".equals(month)){
            cnMonth = "十月";
        }else if("11".equals(month)){
            cnMonth = "十一月";
        }else if("12".equals(month)){
            cnMonth = "十二月";
        }
        dto.setInputTimeCn(cnMonth);
    }

    private void calculate(List<WithdrawCashDto> withdrawCashDtos, Long agentId){
        for(WithdrawCashDto dto : withdrawCashDtos){
            List<TransfersTradeDto> transfersTradeDtos = dto.getTradeList();
            Double alreadyPayCount = 0.0; //已经提现的充值金额
            Double alreadyAmtCount = 0.0; //已经提现金额
            if(null!=transfersTradeDtos && !transfersTradeDtos.isEmpty()){
                SqlParameterDto sqlParameterDto = new SqlParameterDto();
                DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                String endDate = transfersTradeDtos.get(transfersTradeDtos.size()-1).getPaymentTime();
                LocalDate date = LocalDate.parse(endDate,dateTimeFormatter);
                String startDate = date.with(TemporalAdjusters.firstDayOfMonth()).toString();
                sqlParameterDto.setAgentId(agentId);
                sqlParameterDto.setStartDate(startDate);
                sqlParameterDto.setEndDate(endDate);
                List<WithdrawCashDto> subWithdrawCashDtos = withdrawCashDao.queryIndex(sqlParameterDto);
                alreadyPayCount = subWithdrawCashDtos.get(0).getPayCount();
                alreadyAmtCount = transfersTradeDtos.stream().mapToDouble(t -> t.getMoney()).sum();
            }
            Double enablePayCount = ArithUtil.sub(dto.getPayCount(), alreadyPayCount);//可以提现的充值金额
            dto.setEnableAmtCount(ArithUtil.mul(enablePayCount, queryProportion(enablePayCount), 2));
            dto.setAlreadyAmtCount(alreadyAmtCount);
            dto.setCanApply(dto.getEnableAmtCount()>dto.getAlreadyAmtCount());
        }
    }

    //返利比例表：1-2999元，返20%。3000-4999元，返25%。5000-9999元，返35%。10000-19999元，返45%。20000元以上返55%。
    private Double queryProportion(Double amt){
        double ret = 0.0;
        if(amt >=1 && amt<=2999){
            ret = 0.2;
        }else if(amt >=3000 && amt<=4999){
            ret = 0.25;
        }else if(amt >=5000 && amt<=9999){
            ret = 0.35;
        }else if(amt >=10000 && amt<=19999){
            ret = 0.45;
        }else if(amt >=20000){
            ret = 0.55;
        }
        return ret;
    }

}
